package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.model.CurrentUser;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.system.dao.SysPopupRemindDao;
import com.ccp.dev.system.model.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * sys_popup_remind 信息操作服务类
 *
 * @author hw
 * @date 2019-09-17 16:00:35
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysPopupRemindService extends BaseService<SysPopupRemind> {

    @Resource
    private SysPopupRemindDao sysPopupRemindDao;

    /**
     * 启动所选数据
     * @param ids
     * @return
     */
    public int startRemind(String[] ids){
        int result = this.sysPopupRemindDao.startRemind(ids);
        return result;
    }

    /**
     * 停止所选数据
     * @param ids
     * @return
     */
    public int stopRemind(String[] ids){
        int result = this.sysPopupRemindDao.stopRemind(ids);
        return result;
    }

    /**
     * 获取权限信息
     * @param id
     * @return
     */
    public List<SysObjRights> getPopupRemindRights(String id){
        return this.sysPopupRemindDao.getPopupRemindRights(id);
    }

    /**
     * 获取对一个用户生效提醒列表 已过滤了不启动的remind列表
     *
     * @param user
     * @return List<SysPopupRemind>
     * @exception
     * @since 1.0.0
     */
    public List<SysPopupRemind> getByUser(SysUser user) throws ClassNotFoundException {
        //这一步很关键 用伪代码来表达map 的内容就是
        //类型 然后 接着一堆对应的ID eg：
        //user : 1,2,3,4...
        //org : 1,2,3,4...
        //然后 根据上面的对应ID 和 权限配置的ID进行比较，看满不满足权限条件
        Map<String, List<String>> map = getUserRelation(getCurrentUser());

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("relationMap", map);//基本固定
        params.put("objType", SysObjRights.RIGHT_TYPE_POPUP_MSG);

        //开始坚持map中返回的ID符不符合授权设置
        List<SysPopupRemind> sprs = sysPopupRemindDao.getByUser(params);
        return sprs;
//        return null;
    }




    public Map<String, List<String>> getUserRelation(CurrentUser currentUser) {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        try {
            List<ICurUserService> objectList = getUserRelation();
            for (ICurUserService curObj : objectList) {
                List<String> list = curObj.getByCurUser(currentUser);
                map.put(curObj.getKey(), list);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return map;
    }

    public List<ICurUserService> getUserRelation()
            throws ClassNotFoundException {
        Map<String, Object> instMap = AppUtil
                .getImplInstance(ICurUserService.class);
        Collection<Object> instCollect = instMap.values();
        List<ICurUserService> list = new ArrayList<ICurUserService>();
        for (Object obj : instCollect) {
            list.add((ICurUserService) obj);
        }

        return list;
    }

    /**
     * 获取当前用户对象。
     * @return
     */
    public CurrentUser getCurrentUser(){
        SysUser curUser=(SysUser) ContextUtil.getCurrentUser();
        Position pos= (Position) ContextUtil.getCurrentPos();
        SysOrg sysOrg = (SysOrg)ContextUtil.getCurrentOrg();
        CurrentUser currentUser=new CurrentUser();

        currentUser.setUserId(curUser.getUserId());
        currentUser.setAccount(curUser.getAccount());
        currentUser.setName(curUser.getFullName());
        if(pos != null){
            //currentUser.setOrgId(pos.getOrgId());
            currentUser.setPosId(pos.getPosId());
        }
        if(sysOrg!=null){
            currentUser.setOrgId(sysOrg.getOrgId());
        }

        return currentUser;
    }
}
